//
//  JAStatusViewModel.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/6.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit

class JAStatusViewModel: NSObject {
    
    var statusModel: JAStatusModel
    
    /// 处理过的来源字符串
    var sourceStr: String?
    
    /// 处理过的vip等级的图片
    var vipImage: UIImage?
    
    /// 处理过的皇冠等级的图片
    var levelImage: UIImage?
    
    /// 处理过的来源字符串
    var timeStr: String?
    
    ///配图的数组
    var pic_urls: [JAPictureModel]? = []
    
    ///配图的size
    var pictureViewSize: CGSize = CGSize.zero
    
    
    /// 构造函数, viewModel创建时, 就将该处理的数据处理完
    init(statusModel: JAStatusModel) {
        
        self.statusModel = statusModel
        
        super.init()
        
        // 处理来源
        dealWithSource()
        
        // 处理vip的图标
        dealWithVipIcon()
        
        // 处理处理皇冠的等级
        dealWithLevelIcon()
        
        //处理时间字符串
        dealWithTime()
        
        //给配图的数组赋值
        setPicurls()
        
        //计算cell中的配图视图的大小
        caculatePictureViewSize()
    }
    
    /// 根据图片的张数, 确定配图的高度
    func caculatePictureViewSize () {
        
        //图片的宽度
        let imageWH = (screenWidh - 40)/3
        
        //图片的行数
        if let count = pic_urls?.count, count > 0 {
            let rows = (count - 1)/3 + 1 //分页算法
            pictureViewSize = CGSize(width: screenWidh-20, height: CGFloat(rows)*imageWH + CGFloat(rows-1)*10)
        }

    }
    
    ///给配图的数组赋值
    func setPicurls () {
        //原创有配图,转发无配图
        if let count = statusModel.pic_urls?.count, count > 0 {
            pic_urls = statusModel.pic_urls
            return
        }
        
        //原创无配图,判断转发是否有配图
        if let count = statusModel.retweeted_status?.pic_urls?.count, count > 0 {
            pic_urls = statusModel.retweeted_status?.pic_urls
        }
    }
    
    ///将服务器返回的来源字符串,处理成需要的来源字符
    func dealWithSource() {
        if let source = statusModel.source, source.characters.count > 0 {
            let starIndex = source.range(of: "\">")?.upperBound
            let endIndex = source.range(of: "</a>")?.lowerBound
            
            let range = starIndex!..<endIndex!
            sourceStr = source.substring(with: range)
            
        }
    }
    
    
    ///处理vip的等级图标
    func dealWithVipIcon () {
        
        //用户vip的信息: 0代表达人, 2, 3, 5代表企业达人, 220代表草根达人
        if let vip = statusModel.user?.verified_type {
            switch vip {
            case 0:
                vipImage = UIImage(named:"avatar_vip")
            case 2, 3, 5:
                vipImage = UIImage(named:"avatar_enterprise_vip")
            case 220:
                vipImage = UIImage(named:"avatar_grassroot")
            default:
                vipImage = nil
            }
        }
    }
    
    ///处理👑等级的图标
    func dealWithLevelIcon () {
        
        //用户的皇冠的等级 从1到6
        if let mbrank = statusModel.user?.mbrank, mbrank > 0 && mbrank < 7 {
            levelImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
    }
    
     /// 处理时间的字符串
    func dealWithTime() {
        
        if let sinaTime = statusModel.created_at {
            //timeStr = Date.sinaTimeToDate(sinaTime: sinaTime).dateToRequiredTimeStr()
            timeStr = Date.requiredTimeStr(sinaTime: sinaTime)
        }
    }
}
